package typistcal.commandlogic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

//@author A0105620X
public class UserPreference {
	
	private static UserPreference up;

	private static final Logger LOGGER = Logger
			.getLogger(Event.class.getName());
	
	private static int predictDateFirstTaken=-1;
	private static int predictDateOnlyTaken=-1;
	private static final int PREDICT_TAKEN = 3;
	private static final int PREDICT_NOT_TAKEN = 0;
	private static int dateOnly, timeOnly;
	private static int dateFirst, timeFirst;

	public UserPreference(){
		if(predictDateFirstTaken==-1 || predictDateOnlyTaken==-1){
			initializeUserPreference();
		}
	}
	
	public static UserPreference getUserPreference(){
		 if(up!=null){
			 return up;
		 }else{
			 return new UserPreference();
		 }
	}
	
	private void initializeUserPreference() {
		File up = new File("userPreference.txt");

		try {
			Scanner scan = new Scanner(up);

			loadUserHabit(scan);
			predictDateFirstTaken = (dateFirst >= timeFirst) ? PREDICT_TAKEN
					: PREDICT_NOT_TAKEN;
			predictDateOnlyTaken = (dateOnly >= timeOnly) ? PREDICT_TAKEN
					: PREDICT_NOT_TAKEN;

			scan.close();

		} catch (FileNotFoundException e) {
			initializeUserHabitRecord(up);
		}
	}

	private void initializeUserHabitRecord(File up) {
		try {
			up.createNewFile();
		} catch (IOException e1) {
			LOGGER.log(Level.SEVERE,
					"User preference initialization failed");
		}
		PrintWriter pw;
		try {
			pw = new PrintWriter(up);
			pw.write("Date only input: 0\nTime only input: 0\nDate first input: 0\nTime first input: 0");
			pw.close();
		} catch (FileNotFoundException e1) {
			LOGGER.log(Level.SEVERE,
					"User preference initialization failed");
		}
		dateOnly = 0;
		timeOnly = 0;
		dateFirst = 0;
		timeFirst = 0;
	}

	private void loadUserHabit(Scanner scan) {
		while (scan.hasNextLine()) {
			String data = scan.nextLine();
			if (data.contains("Date only input:")) {
				data = data.replace("Date only input:", "").trim();
				dateOnly = Integer.parseInt(data);
			} else if (data.contains("Time only input:")) {
				data = data.replace("Time only input:", "").trim();
				timeOnly = Integer.parseInt(data);
			} else if (data.contains("Date first input:")) {
				data = data.replace("Date first input:", "").trim();
				dateFirst = Integer.parseInt(data);
			} else if (data.contains("Time first input:")) {
				data = data.replace("Time first input:", "").trim();
				timeFirst = Integer.parseInt(data);
			}
		}
	}
	
	public void updateUserPreference() {
		PrintWriter pw;
		try {
			pw = new PrintWriter("userPreference.txt");
			pw.write("Date only input: " + dateOnly + "\n");
			pw.write("Time only input: " + timeOnly + "\n\n");
			pw.write("Date first input: " + dateFirst + "\n");
			pw.write("Time first input: " + timeFirst + "\n");

			pw.close();
		} catch (FileNotFoundException e1) {
			LOGGER.log(Level.SEVERE, "User preference update failed");
		}
	}

	public boolean isDateFirstPrediction(){
		return predictDateFirstTaken == PREDICT_TAKEN
				|| predictDateFirstTaken == PREDICT_TAKEN - 1;
	}
	
	public void updateAfterDateFirstPrediction(boolean isCorrectPrediction){
		if (isCorrectPrediction) {
			if (predictDateFirstTaken == PREDICT_TAKEN - 1) {
				predictDateFirstTaken = PREDICT_TAKEN;
			}
			dateFirst++;
		} else {
			predictDateFirstTaken--;
			timeFirst++;
		}
	}
	
	public void updateAfterTimeFirstPrediction(boolean isCorrectPrediction){
		if (isCorrectPrediction) {
			if (predictDateFirstTaken == PREDICT_NOT_TAKEN + 1) {
				predictDateFirstTaken = PREDICT_NOT_TAKEN;
			}
			timeFirst++;
		} else {
			predictDateFirstTaken++;
			dateFirst++;
		}
	}
	
	public boolean isDateOnlyPrediction(){
		return predictDateOnlyTaken == PREDICT_TAKEN
				|| predictDateOnlyTaken == PREDICT_TAKEN - 1;
	}
	
	public void updateAfterDateOnlyPrediction(boolean isCorrectPrediction){
		if (isCorrectPrediction) {
			if (predictDateOnlyTaken == PREDICT_TAKEN - 1) {
				predictDateOnlyTaken = PREDICT_TAKEN;
			}
			dateOnly++;
		} else {
			predictDateOnlyTaken--;
			timeOnly++;
		}
	}
	
	public void updateAfterTimeOnlyPrediction(boolean isCorrectPrediction){
		if (isCorrectPrediction) {
			if (predictDateOnlyTaken == PREDICT_NOT_TAKEN + 1) {
				predictDateOnlyTaken = PREDICT_NOT_TAKEN;
			}
			timeOnly++;
		} else {
			predictDateOnlyTaken++;
			dateOnly++;
		}
	}
}
